{-# LANGUAGE DeriveDataTypeable #-}

module Language.Dependencies.Adaptors.Html where

-- container
import Data.Tree (Tree(Node,rootLabel))

-- syb
import Data.Generics (Data)

--import Language.Dependencies.DataTree (data2tree)
import Language.Dependencies.Term2tree (data2tree)

-- local imports
import Language.Dependencies.Language

-- Parsec (CSV Parser)
import qualified Text.ParserCombinators.Parsec as Parsec

import Language.HtmlParsec.HtmlParsec
import Language.HtmlParsec.Syntax

 
import Data.Generics hiding (Infix)
import qualified Text.ParserCombinators.Parsec.Token as P
import Text.ParserCombinators.Parsec.Language (javaStyle)
import Text.ParserCombinators.Parsec.Expr

languagehtml = Language 
            { name=  "Html" 
            , syntax = "html" 
            , exts = [".html"] 
            , parse = parseHtml
            , toTree = (data2tree TheHtml :: Html -> Tree String)
            , srcLoc       = toSrcLoc
            , adjustSrcLoc = Nothing
            , srcLoctype   = (OffSet undefined undefined) 
            , typerep = Just (TheHtml)
            }
             
parseHtml :: String -> Either Error Html
parseHtml s = case (Parsec.parse htmlParser "default" (parseTags' s)) of
   Right p -> Right p
   _       -> Left Err


toSrcLoc :: Tree String -> [Loc]
toSrcLoc (Node ('P':' ':label) cs ) = [OffSet (read l :: Int) (read r :: Int)] 
  where
    [l,r] = words label
toSrcLoc _ = []
